export class Socket {
  webSocket = "";
  setIntervalWesocketPush = null;
  URL = "";

  /**
 * 建立websocket连接
 * @param {string} url ws地址
 */
  createSocket(url) {
    console.log("连接", url);
    if (url) {
      this.URL = url;
    }
    this.websocket && this.websocket.close();
    if (!this.websocket) {
      if (this.URL) {
        console.log("建立websocket连接");
        this.websocket = new WebSocket(this.URL);
        this.websocket.onopen = this.onopenWS;
        this.websocket.onmessage = this.onmessageWS;
        this.websocket.onerror = this.onerrorWS;
        this.websocket.onclose = this.oncloseWS;
      }
    } else {
      console.log("websocket已连接");
    }
  }
  /** 打开WS之后发送心跳 */
  onopenWS =() => {
    this.sendPing();
  }

  /** 连接失败重连 */
  onerrorWS = () => {
    this.websocket.close();
    clearInterval(this.setIntervalWesocketPush);
    console.log("连接失败重连中", this.setIntervalWesocketPush);
    console.log(this.websocket.readyState, "Socket.readyState");
    if (this.websocket.readyState !== 3) {
      this.websocket = null;
      this.createSocket();
    }
  }
  /** WS数据接收统一处理 */
  onmessageWS = (e) => {
    console.log(e, "eee");
    window.dispatchEvent(new CustomEvent("onmessageWS", { detail: { data: e.data }}));
  }

  /**
   * 发送数据但连接未建立时进行处理等待重发
   * @param {any} message 需要发送的数据
   */
  connecting = (message) => {
    console.log("connecting", message);
    setTimeout(() => {
      if (this.websocket.readyState === 0) {
        this.connecting(message);
      } else {
        this.websocket.send(message);
      }
    }, 1000);
  }

  /**
   * 发送数据
   * @param {any} message 需要发送的数据
   */
  sendWSPush = (message) => {
    console.log(message, "sendWSPush");
    if (this.websocket !== null && this.websocket.readyState === 3) {
      this.websocket.close();
      this.createSocket();
    } else if (this.websocket.readyState === 1) {
      this.websocket.send(message);
    } else if (this.websocket.readyState === 0) {
      this.connecting(message);
    }
  }
  /** 断开重连 */
  oncloseWS =() => {
    console.log("断开重连", "oncloseWS", this.websocket.readyState);
    clearInterval(this.setIntervalWesocketPush);
    console.log(this.setIntervalWesocketPush, "this.setIntervalWesocketPush");
    if (this.websocket.readyState !== 2) {
      this.websocket = null;
      setTimeout(() => {
        this.createSocket();
      }, 2000);
    }
  }
  /** 发送心跳
   * @param {number} time 心跳间隔毫秒 默认5000
   * @param {string} ping 心跳名称 默认字符串ping
   */
  sendPing = (time = 5000, ping = "ping") => {
    console.log("sendPing", time, this.setIntervalWesocketPush);
    clearInterval(this.setIntervalWesocketPush);
    this.websocket.send(ping);
    this.setIntervalWesocketPush = setInterval(() => {
      this.websocket.send(ping);
    }, time);
  }

  closeWS = () => {
    console.log("中断", this.websocket.close);
    this.websocket.close && this.websocket.close();
    this.websocket = null;
    clearInterval(this.setIntervalWesocketPush);
    this.URL = null;
  };
}

